package com.yu.mall.controller;

import com.yu.mall.dto.BaseDTO;
import com.yu.mall.dto.address.AddressDTO;
import com.yu.mall.entity.Address;
import com.yu.mall.exception.ServiceException;
import com.yu.mall.service.AddressService;
import com.yu.mall.util.JDUtils;
import com.yu.mall.util.ResultUtils;
import com.yu.mall.vo.ResultVO;
import com.yu.mall.vo.SelectVO;
import com.yu.mall.vo.address.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("address")
@Api(tags = "地址管理",value = "地址管理")
public class AddressController {

    @Autowired
    private AddressService addressService;


    @GetMapping("listProvince")
    @ApiOperation(value = "获取省信息")
    public ResultVO<List<SelectVO>> listProvince() throws ServiceException {
        return ResultUtils.success(JDUtils.getProvince());
    }


    @GetMapping("listCity")
    @ApiOperation(value = "获取市信息")
    @ApiImplicitParam(name = "provinceId",value="省id",required=true)
    public ResultVO<List<SelectVO>> listCity(String provinceId) throws ServiceException {
        return ResultUtils.success(JDUtils.getCity(provinceId));
    }

    @GetMapping("listCounty")
    @ApiOperation(value = "获取区信息")
    @ApiImplicitParam(name = "cityId",value="市id",required=true)
    public ResultVO<List<SelectVO>> listCounty(String cityId) throws ServiceException {
        return ResultUtils.success(JDUtils.getCounty(cityId));
    }


    @GetMapping("listTown")
    @ApiOperation(value = "获取第四级信息")
    @ApiImplicitParam(name = "countyId",value="区id",required=true)
    public ResultVO<List<SelectVO>> listTown(String countyId) throws ServiceException {
        return ResultUtils.success(JDUtils.getTown(countyId));
    }

    @PostMapping("add")
    @ApiOperation(value = "添加地址")
    public ResultVO add(AddressDTO addressDTO){
        return addressService.add(addressDTO);
    }

    @PostMapping("update")
    @ApiOperation(value = "修改地址")
    public ResultVO update(AddressDTO addressDTO){
        return addressService.update(addressDTO);
    }

    @GetMapping("list")
    @ApiOperation(value = "查询地址列表")
    public ResultVO<List<AddressVO>> list(BaseDTO baseDTO){
        return addressService.list(baseDTO.getOpenid());
    }

    @GetMapping("getDefault")
    @ApiOperation(value = "查询默认地址")
    public ResultVO<AddressVO> getDefault(BaseDTO baseDTO){
        return addressService.getDefault(baseDTO.getOpenid());
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除地址")
    @ApiImplicitParam(name = "id",value="id",required=true)
    public ResultVO delete(Integer id){
        Address address = new Address();
        address.setIsDelete("1");
        address.setId(id);
        int i = addressService.updateByPrimaryKeySelective(address);
        if(i<= 0){
            return ResultUtils.error("删除失败");
        }
        return ResultUtils.success();
    }
}
